<template>
  <div class="loghub">
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/index' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>操作日记</el-breadcrumb-item>
      <el-breadcrumb-item>日记列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card class="box_card">
      <!-- 顶部搜索区域 -->
      <el-row :gutter="20">
        <el-col :span="8">
          <el-date-picker
            v-model="log_time_search"
            @change="logtime()"
            type="date"
            placeholder="选择日期时间"
            value-format="yyyy-MM-dd"
          >
          </el-date-picker>
        </el-col>
      </el-row>
      <!-- 日记列表区 -->
      <el-table :data="loglist" border stripe class="tabel_box">
        <el-table-column type="index" label="#"></el-table-column>
        <el-table-column label="操作对象" prop="log_user"></el-table-column>
        <el-table-column label="操作内容" prop="log_content"></el-table-column>
        <el-table-column label="操作时间" prop="log_time"></el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button
              type="danger"
              icon="el-icon-delete"
              circle
              size="mini"
              @click="dellog(scope.row.log_id)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页区 -->
      <div class="page_box" v-if="ispage">
        <el-pagination
          :background="true"
          :page-count="pager_count"
          @current-change="handleCurrentChange"
          :layout="col"
          :total="count_total"
        >
        </el-pagination>
      </div>
    </el-card>
  </div>
</template>

<script>
export default {
  data() {
    return {
      loglist: [], //日记列表
      log_time_search: "", //时间搜索
      pager_count: 1, // 当前页码
      count_total: 0, // 总页数
      col: "prev, pager, next, jumper,slot", //开启的配置
      ispage: true, // 是否显示分页
    };
  },
  mounted() {
    this.getlogInfo(); //获取日记列表
  },
  watch: {
    // 如果只有1页，则不显示分页按钮
    count_total(newVal) {
      if (newVal <= 10) {
        this.ispage = false;
      }
    },
  },
  methods: {
    funloginfo(parameter) {
      this.$getapi("/log/get", parameter, "json")
        .then((res) => {
          if (res.code === 200) {
            this.loglist = res.data;
            this.count_total = res.count * 10;
          } else {
            this.$message.error(res.msg);
          }
        })
        .catch((error) => {
          this.$message.error(error);
        });
    },
    // 获取操作日记列表
    getlogInfo() {
      let par = {
        page: this.pager_count,
      };
      this.funloginfo(par);
    },
    // 操作时间搜索
    logtime() {
      let par = { log_time: this.log_time_search };
      this.funloginfo(par);
    },
    // 页数跳转
    handleCurrentChange(val) {
      this.pager_count = val;
      this.getlogInfo();
    },
    // 删除日记
    dellog(log_id) {
      this.$deleteapi("/log/del", { log_id: log_id }, "json")
        .then((res) => {
          if (res.code === 200) {
            this.$message.success(res.msg);
            this.getlogInfo();
          } else {
            this.$message.error(res.msg);
          }
        })
        .catch((error) => {
          this.$message.error(error);
        });
    },
  },
};
</script>

<style lang="less" scoped>
.loghub {
  .box_card {
    margin-top: 20px;
    .tabel_box {
      font-size: 14px;
    }
    .page_box {
      margin-top: 20px;
    }
  }
  .el-row {
    margin-bottom: 10px;
  }
}
</style>